package org.gjy.m8.stream.collect;

import org.gjy.m8.stream.Student;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-11-04 10:46:38
 */
public class StudentCollect implements Collector<Student, Map<Integer, List<Student>>, Map<Integer, List<Student>>> {

    @Override
    public Supplier<Map<Integer, List<Student>>> supplier() {
        return HashMap::new;
    }

    @Override
    public BiConsumer<Map<Integer, List<Student>>, Student> accumulator() {
        return (map, list) -> map.computeIfAbsent(list.getAge(), k -> new ArrayList<>()).add(list);
    }

    @Override
    public BinaryOperator<Map<Integer, List<Student>>> combiner() {
        return (l, r) -> {
            l.putAll(r);
            return l;
        };
    }

    @Override
    public Function<Map<Integer, List<Student>>, Map<Integer, List<Student>>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
    }

}
